package com.min.star.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.min.star.model.Student;
import com.min.star.model.Systeminfo;
import com.min.star.model.Teacher;
import com.min.star.model.Types;
import com.min.star.service.IStudentService;
import com.min.star.service.ISysteminfoService;
import com.min.star.service.ITeacherService;


@Controller
@RequestMapping("/login")
public class LoginController {
	
	@Autowired
	private IStudentService studentService;
	
	@Autowired
	private ITeacherService teacherService;
	
	@Autowired
	private ISysteminfoService systemService;
	
	@RequestMapping("/login")
	public String login(Types types, HttpSession session, Model model) {
		List<Systeminfo> systeminfos = this.systemService.selectSystemList();
		session.setAttribute("systeminfos", systeminfos.get(0));
		if (types.getType() == 1) {
			Student student = new Student(types.getId(), types.getPassword());
			Student stu = this.studentService.login(student);
			if(stu != null) {
				System.out.println(stu.toString());
				session.setAttribute("student", stu);
				return "/student/student";
			}else {
				session.setAttribute("msg", "学号或密码错误！");
				return "login";
			}
		}else if (types.getType() == 2) {
			Teacher teacher = new Teacher(types.getId(), types.getPassword());
			Teacher tea = this.teacherService.login(teacher);
			if(tea != null) {
				session.setAttribute("teacher", tea);
				return "/teacher/teacher";
			}else {
				session.setAttribute("msg", "工号或密码错误！");
				return "login";
			}
		}else {
			Teacher admin = new Teacher(types.getId(), types.getPassword());
			Teacher ad = this.teacherService.login(admin);
			if(ad != null && ad.getTeacherName().equalsIgnoreCase("admin")) {
				session.setAttribute("admin", ad);
				return "/admin/admin";
			}else {
				session.setAttribute("msg", "特殊账号或密码错误！");
				return "login";
			}
		}
	}
}
